<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="pageParams.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="upInput" />
        <!-- 树形组件 -->

        <el-tree
          :data="depts"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          node-key="id"
          :current-node-key="currentNodeKey"
          @node-click="nodeClick"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">

          <el-button size="mini" type="primary" @click="$router.push(`/employee/detai`)">添加员工</el-button>
          <el-button size="mini" @click="showExcel">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>

        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{ row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{ row }">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detai/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="getListEna(row.id)">角色</el-button>
              <el-popconfirm title="确认删除该行数据吗？" @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :page-size="pageParams.pagesize"
            :current-page="pageParams.page"
            @current-change="currentChange"
          />
        </el-row>
      </div>
      <ImportExcel :show-excel-dialog.sync="showExcelDialog" @updateEmployee="getEmployeeList" />
      <el-dialog
        title="角色分配"
        :visible.sync="dialogVisible"
        width="40%"
      >
        <el-checkbox-group v-model="roleIds">
          <el-checkbox v-for="item in checkList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
        <span slot="footer" class="dialog-footer" style="margin-right:110px">
          <el-button type="primary" @click="addAssignRoles">确 定</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartment } from '@/api/department'
import { getEmployeeList, exportEmployee, delEmployee, getListEnabled, getSysUser, putAssignRoles } from '@/api/employee'
import { transformData } from '@/utils'
import ImportExcel from './components/import-excel.vue'
import FileSaver from 'file-saver'
export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {

      checkList: [], // 多选框的集合
      roleIds: [], //

      depts: [], // 组织数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      // 存储查询参数
      queryParams: {
        departmentId: null
      },
      currentNodeKey: 1,
      list: [],
      showExcelDialog: false,

      pageParams: {
        page: 1, // 当前页码
        pagesize: 5, // 每页几条数据
        keyword: '', // 模糊搜索，
        departmentId: 1// 部门id,根据部门查询当前部门及子部门的用户
      },
      total: 0, // 记录总页数
      dialogVisible: false

    }
  },
  created() {
    this.getDepartment()
    this.getEmployeeList()
  },
  methods: {
    async getDepartment() {
      const res = await getDepartment()
      // 递归方法 将列表转化成树形
      // let result = await getDepartment()
      this.depts = transformData(res.data, 0)
      // this.queryParams.departmentId = this.depts[0].id
      // 设置选中节点
      // 树组件渲染是异步的 等到更新完毕
      // this.$nextTick(() => {
      //   // 此时意味着树渲染完毕
      //   this.$refs.deptTree.setCurrentKey(this.queryParams.departmentId)
      // })
    },
    async getListEna(id) {
      this.id = id
      const { data } = await getListEnabled()
      this.checkList = data

      const res = await getSysUser(id)
      this.roleIds = res.data.roleIds
      this.dialogVisible = true
    },
    async  addAssignRoles() {
      await putAssignRoles(this.id, this.roleIds)
      this.$message.success('分配成功')
      this.dialogVisible = false
    },
    // 获取员工信息
    async getEmployeeList() {
      const result = await getEmployeeList(this.pageParams)
      this.list = result.data.rows
      this.total = result.data.total
    },
    // 分页
    currentChange(page) {
      this.pageParams.page = page
      this.getEmployeeList()
    },
    // 实现模糊搜素
    upInput() {
      clearTimeout(this.time)
      this.time = setTimeout(() => {
        this.getEmployeeList()
      }, 1000)
    },
    nodeClick(row) {
      // 将他的页数重置为1
      this.pageParams.page = 1
      // 点击部门节点 id
      this.pageParams.departmentId = row.id
      // 刷新数据
      this.getEmployeeList()
    },
    // 文件导出
    async exportEmployee() {
      const result = await exportEmployee()
      console.log(result, 1111)
      FileSaver.saveAs(result, '员工信息.xlsx')
    },
    showExcel() {
      this.showExcelDialog = true
    },
    // 删除
    async confirmDel(id) {
      // 发送请求
      await delEmployee(id)
      // 提示用户
      this.$message.success('删除成功')
      if (this.list.length === 1 && this.pageParams.page > 1) {
        this.pageParams.page--
      }
      // 刷新数据
      this.getEmployeeList()
    }

  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;

    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
